#!/bin/bash

set -x
set -e
sed -i 's/\r//' ./config/config
sed -i 's/\r//' ./script/lib.sh
sed -i 's/\r//' ./script/html.sh
sed -i 's/\r//' ./script/es.sh
sed -i 's/\r//' ./script/mariadb.sh
sed -i 's/\r//' ./check.sh

source ./config/config
source ./script/html.sh
source ./script/lib.sh

############## 开始执行巡检 ###############
#linux（通过在虚拟机执行的命令）
if [[ "${ANSIBLE_LINUX_ENABLED}" = "true" ]]; then
   sh ./script/linux.sh
fi

#node(通过kubectl和node-exporter调取的接口)
bash ./script/node.sh

#pod
bash ./script/pod.sh

#rabbitmq
bash ./script/rabbitmq.sh

#es
bash ./script/es.sh

#redis
bash ./script/redis.sh

#mariadb
bash ./script/mariadb.sh


## 获取各类型结果
FATAL_LIST=$(grep -r -l "FATAL" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)
ERROR_LIST=$(grep -r -l "ERROR" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)
NORMAL_LIST=$(grep -r -l "NORMAL" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)
UNKOWN_LIST=$(grep -r -l "UNKOWN" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)
WARNING_LIST=$(grep -r -l "WARNING" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)

##>>>>>>>>>>>>>>>>>创建html
create_html_css
create_a_head

# 左侧菜单栏
create_a_menu_head
# node
create_one_level_menu_item "NODE CHECKLIST"
for key in ${node_arr[*]}
do
	create_two_level_menu_item "$key" "${node_dict[$key]}"
done
create_one_level_menu_item_end

# POD
create_one_level_menu_item "POD CHECKLIST"
for key in ${pod_arr[*]}
do
	create_two_level_menu_item "$key" "${pod_dict[$key]}"
done
create_one_level_menu_item_end

# RABBITMQ
create_one_level_menu_item "RABBITMQ CHECKLIST"
for key in ${rabbitmq_arr[*]}
do
	create_two_level_menu_item "$key" "${rabbitmq_dict[$key]}"
done
create_one_level_menu_item_end

# ES
create_one_level_menu_item "ES CHECKLIST"
for key in ${es_arr[*]}
do
	create_two_level_menu_item "$key" "${es_dict[$key]}"
done
create_one_level_menu_item_end

# REDIS
create_one_level_menu_item "REDIS CHECKLIST"
for key in ${redis_arr[*]}
do
	create_two_level_menu_item "$key" "${redis_dict[$key]}"
done
create_one_level_menu_item_end

# MYSQL
create_one_level_menu_item "MYSQL CHECKLIST"
for key in ${mariadb_arr[*]}
do
	create_two_level_menu_item "$key" "${mariadb_dict[$key]}"
done
create_one_level_menu_item_end

create_a_menu_end


create_a_content_box
create_a_headline
create_scroll_to_top


####第一个章节
#create_html_head2 "巡检环境基本信息"
#create_table_head "importance_info"

#### 第二个章节
create_html_head2 "巡检结果列表"
echo -e "<div id='db_server' style='transition: height 0.2s;'>">>$file_output
create_table_head "result_info"
create_tr3_h "Number" "Status" "Type" "Message" "Details"
create_db_server_info ${FATAL_LIST[*]} "FATAL"
create_db_server_info ${ERROR_LIST[*]} "ERROR"
create_db_server_info ${WARNING_LIST[*]} "WARNING"
create_db_server_info ${UNKOWN_LIST[*]} "UNKOWN"
create_db_server_info ${NORMAL_LIST[*]} "NORMAL"
create_table_end_no_br
echo -e "<div id='foldtable' style='width: 94%;' class='foldbtn'>展开</div>">>$file_output
echo -e "</div></br>">>$file_output

#### 第三个章节
create_html_head2 "操作系统巡检项详情"
for key in ${node_arr[*]}
do
	#create_a_hr
	create_html_head3 "$key" "${node_dict[$key]}" # 巡检项标题
	create_table_head_2 # 创建表头
	detail_file_name="$result_dir/$key""_detail"
	create_tr_detail "$detail_file_name" "$key"
	create_table_end
	create_suggestion "$key" "${node_suggestion[$key]}"
done


create_html_head2 "pod巡检项详情"
for key in ${pod_arr[*]}
do
	#create_a_hr
	create_html_head3 "$key" "${pod_dict[$key]}" # 巡检项标题
	create_table_head_2 # 创建表头
	detail_file_name="$result_dir/$key""_detail"
	create_tr_detail "$detail_file_name" "$key"
	create_table_end
	create_suggestion "$key" "${pod_suggestion[$key]}"
done

create_html_head2 "rabbitmq巡检项详情"
for key in ${rabbitmq_arr[*]}
do
	#create_a_hr
	create_html_head3 "$key" "${rabbitmq_dict[$key]}" # 巡检项标题
	create_table_head_2 # 创建表头
	detail_file_name="$result_dir/$key""_detail"
	create_tr_detail "$detail_file_name" "$key"
	create_table_end
	create_suggestion "$key" "${rabbitmq_suggestion[$key]}"
done

create_html_head2 "es巡检项详情"
for key in ${es_arr[*]}
do
#	echo $key
#	echo "${es_dict[$key]}"
	create_html_head3 "$key" "${es_dict[$key]}" # 巡检项标题
	create_table_head_2 # 创建表头
	detail_file_name="$result_dir/$key""_detail"
	create_tr_detail "$detail_file_name" "$key"
	create_table_end
	create_suggestion "$key" "${es_suggestion[$key]}"
done


create_html_head2 "redis巡检项详情"
for key in ${redis_arr[*]}
do
	#create_a_hr
	create_html_head3 "$key" "${redis_dict[$key]}" # 巡检项标题
	create_table_head_2 # 创建表头
	detail_file_name="$result_dir/$key""_detail"
	create_tr_detail "$detail_file_name" "$key"
	create_table_end
	create_suggestion "$key" "${redis_suggestion[$key]}"
done


create_html_head2 "mysql巡检项详情"
for key in ${mariadb_arr[*]}
do
	#create_a_hr
	create_html_head3 "$key" "${mariadb_dict[$key]}" # 巡检项标题
	create_table_head_2 # 创建表头
	detail_file_name="$result_dir/$key""_detail"
	create_tr_detail "$detail_file_name" "$key"
	create_table_end
	create_suggestion "$key" "${mariadb_suggestion[$key]}"
done


create_main_content_end
create_html_end

echo -e "\033[93m 巡检结果文件为:$file_output \033[0m"

#推送check文件到minio
chmod +x ./script/mc.sh
./script/mc.sh
